﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace DailyStrips.Model.ImageProvideStrategies
{
  public abstract class FallbackImageProviderStrategy : IImageProviderStrategy
  {
    public IImageProviderStrategy FallbackStrategy { get; private set; }

    public FallbackImageProviderStrategy()
      :this(new NullImageProviderStrategy())
    { }

    public FallbackImageProviderStrategy(IImageProviderStrategy fallbackStrategy)
    {
      FallbackStrategy = fallbackStrategy;
    }

    #region IImageProviderStrategy Members

    public ImageSource GetImage(DateTime date)
    {
      if (IsImageAvailable(date))
      {
        return HandleGetImage(date);
      }
      else 
      {
        var result = FallbackStrategy.GetImage(date);

        HandleFallbackResult(date, result);

        return result;
      }
    }

    public abstract bool IsImageAvailable(DateTime date);

    #endregion

    protected abstract void HandleFallbackResult(DateTime date, ImageSource result);
    protected abstract ImageSource HandleGetImage(DateTime date);
  }
}
